/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.JPanel;

import controllers.entity.CanBo;
import controllers.process.CanBoProcess;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import models.CanBoDAO;
import models.DBConnect;
import views.CanBoFrame;
/**
 *
 * @author HoaDuyen
 */
public class ChucNangCB extends javax.swing.JPanel {

    /**
     * Creates new form ChucNangCB
     */
    private JFrame window;
    private Integer ma=0;
    DefaultTableModel tblModel;
    private int i=0;
    public ChucNangCB(JFrame win) {
        initComponents();
        this.window=win;
        tblModel = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int row, int column) {
            //all cells false
            return false;
        }
        };
        loadTableDataThongKeNhanVien();
        ListSelectionModel listMod = tblData.getSelectionModel();
        listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
       
    }
     
     
    public void valueChanged(ListSelectionEvent e) {
        int maxRows;
        int[] selRows;
        Object value;
        if (!e.getValueIsAdjusting()) {
            selRows = tblData.getSelectedRows();

            if (selRows.length > 0) {
                
                    TableModel tm = tblData.getModel();
                    value = tm.getValueAt(selRows[0], 0);
                    Integer ma=Integer.parseInt(value.toString());
                    window.setEnabled(false);
            }
        }
    }
    public void Show()
    {
        this.setBounds(160, 0, 800, 600);
        window.add(this);
        this.setVisible(true);
        
    }
    public void loadTableDataThongKeNhanVien() {
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return;
        }
        List<CanBo> canboLis = CanBoDAO.getAllCanBo();
		tblModel = new DefaultTableModel() {
                @Override
                public boolean isCellEditable(int row, int column) {
                    //all cells false
                    return false;
                }
                };
		tblModel.addColumn("Mã cán bộ");
		tblModel.addColumn("Họ tên");
                tblModel.addColumn("Số điện thoại");
                tblModel.addColumn("Chức danh");
                tblModel.addColumn("Chức vụ");
                tblModel.addColumn("Khoa viện");
                tblModel.addColumn("Đảng viên");
		 for (CanBo category : canboLis) {
		    Vector<String> row = new Vector<>();
		    row.addElement(String.valueOf(category.getMaCanBo()));
		    row.addElement(String.valueOf(category.getHoTen()));
                    row.addElement(String.valueOf(category.getSoDienThoai()));
                    row.addElement(String.valueOf(category.getChucDanh()));
                    row.addElement(String.valueOf(category.getChucVu()));
                    row.addElement(String.valueOf(category.getKhoaVien()));
                    if(category.getDangVien())row.addElement("Có");
                    else row.addElement("Không");
		    tblModel.addRow(row);
		}
		tblData.setModel(tblModel);
               
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblData = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        indscanbo = new javax.swing.JButton();
        bthemcanbo = new javax.swing.JButton();
        btXoa = new javax.swing.JButton();
        btXem = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(102, 204, 255));
        setMaximumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));

        jScrollPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mouseListener(evt);
            }
        });

        tblData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblData.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                valueCickl(evt);
            }
        });
        jScrollPane1.setViewportView(tblData);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setText("Danh Sách Cán Bộ");

        indscanbo.setBackground(new java.awt.Color(204, 255, 102));
        indscanbo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_save.png"))); // NOI18N
        indscanbo.setText("In danh sách cán bộ");
        indscanbo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                indscanboActionPerformed(evt);
            }
        });

        bthemcanbo.setBackground(new java.awt.Color(204, 255, 102));
        bthemcanbo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/addemp.png"))); // NOI18N
        bthemcanbo.setText("Thêm cán bộ");
        bthemcanbo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bthemcanboActionPerformed(evt);
            }
        });

        btXoa.setBackground(new java.awt.Color(204, 255, 102));
        btXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_delete.png"))); // NOI18N
        btXoa.setText("Xóa Cán Bộ");
        btXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaActionPerformed(evt);
            }
        });

        btXem.setBackground(new java.awt.Color(204, 255, 102));
        btXem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/employee.png"))); // NOI18N
        btXem.setText("Xem thông tin");
        btXem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btXem, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(bthemcanbo, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(41, 41, 41)
                        .addComponent(indscanbo)
                        .addGap(83, 83, 83))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 791, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(378, 378, 378)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(284, 284, 284)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(82, 82, 82)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(indscanbo, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bthemcanbo, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btXem, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(108, 108, 108))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bthemcanboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bthemcanboActionPerformed
      //window.setVisible(false);
     
        window.setEnabled(false);
      CanBoFrame canboP=new CanBoFrame(window,this);
      canboP.setVisible(true);
    }//GEN-LAST:event_bthemcanboActionPerformed

    private void indscanboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_indscanboActionPerformed
        i=0;
        ActionListener printAction = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    i++;
                    if (i==1){
                        tblData.print(JTable.PrintMode.FIT_WIDTH);
                    }
                    
                } catch (PrinterException pe) {
                    System.err.println("Error printing: " + pe.getMessage());
                }
            }
        };
        indscanbo.addActionListener(printAction);
    }//GEN-LAST:event_indscanboActionPerformed

    private void mouseListener(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mouseListener
       
    }//GEN-LAST:event_mouseListener

    private void valueCickl(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_valueCickl

    }//GEN-LAST:event_valueCickl

    private void btXemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXemActionPerformed
        int dem = tblData.getSelectedRow();
        if (dem == -1) {
            JOptionPane.showMessageDialog(this,"Chọn cán bộ cần xem thông tin trước !");
            return;
        }
       
                
        TableModel tm = tblData.getModel();
        Object value = tm.getValueAt(dem, 0);
        int macanbo=Integer.parseInt(value.toString());
        window.setEnabled(false);
        TTCanBo canboP=new TTCanBo(this,window,macanbo);
        canboP.setVisible(true);
        this.setVisible(true);
        //canbo.setVisible(true);
    }//GEN-LAST:event_btXemActionPerformed

    private void btXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaActionPerformed
        int dem = tblData.getSelectedRow();
        if (dem == -1) {
            JOptionPane.showMessageDialog(this,"Chọn cán bộ cần xóa  trước !");
            return;
        }
       
                
        TableModel tm = tblData.getModel();
        Object value = tm.getValueAt(dem, 0);
        int macanbo=Integer.parseInt(value.toString());
        window.setEnabled(false);
        if (JOptionPane.showConfirmDialog(this, "Bạn chắc chắn muốn xóa ?",
                "Xoa Cán Bộ", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            CanBoProcess.deleteCanBo(macanbo);
            this.loadTableDataThongKeNhanVien();
            
        }
        window.setEnabled(true);
        this.setVisible(true);
    }//GEN-LAST:event_btXoaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btXem;
    private javax.swing.JButton btXoa;
    private javax.swing.JButton bthemcanbo;
    private javax.swing.JButton indscanbo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblData;
    // End of variables declaration//GEN-END:variables

   
}
